#!/bin/bash

export
export PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export LD_LIBRARY_PATH=""

THIS_AAF_PACKAGE="aaf-xrootd"
THIS_DIR=`pwd`
AAF_DIR="`cd ../../../../ ; pwd`"
AAF_XROOTD_VER=`basename $THIS_DIR`
AAF_XROOTD_NAME="xrootd_$AAF_XROOTD_VER"
AAF_XROOTD_VER=${AAF_XROOTD_VER:0:3}
echo " Installing $THIS_AAF_PACKAGE on `hostname -f` ..."

if [ ! -e $AAF_DIR/aaf.cf ];then
  echo "$AAF_DIR/aaf.cf was not found!!! Skipping postinstall for AAF ..."
  exit 0
fi

source $AAF_DIR/aaf.cf

if [ ! -z $ALICE_PROOF_AAF_CUSTOM_PATH ]; then
  export PATH="$ALICE_PROOF_AAF_CUSTOM_PATH:$PATH"
  echo "NEW PATH is $PATH"
fi

function InstallExtraSECSSL() {
  rm -f xrootd-secssl-4.3.0.tar.gz > /dev/null 2>&1
#   wget http://skafinfo.saske.sk/alice/files/xrootd-secssl-4.3.0.tar.gz
  wget http://mon1.saske.sk/alice/xrootd-secssl-4.3.0.tar.gz
  tar xfz xrootd-secssl-4.3.0.tar.gz
  cd xrootd-secssl-4.3.0
#   ./configure --prefix=$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME --with-xrootd-location=$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME --libdir=$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/lib64
  ./configure --prefix=$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME --with-xrootd-incdir=$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/include/xrootd --with-xrootd-libdir=$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/lib64 --libdir=$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/lib64
  make 
  make install
}

function InstallXrootdVMSS() {


#   mkdir -p $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME
#   ln -s $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/lib64 $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/lib
  rm -f $ALICE_PROOF_AAF_TMP_DIR/xrd-installer > /dev/null 2>&1
  rm -Rf $ALICE_PROOF_AAF_TMP_DIR/xrd/* > /dev/null 2>&1
  cd $ALICE_PROOF_AAF_TMP_DIR/ && wget http://project-arda-dev.web.cern.ch/project-arda-dev/xrootd/tarballs/installbox/xrd-installer && chmod +x xrd-installer
  cd $ALICE_PROOF_AAF_TMP_DIR/ && ./xrd-installer --prefix=$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME --version=aaf_$AAF_XROOTD_VER --install

  # replace system.cnf
  mv $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrootd/system.cnf $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrootd/system.cnf.orig
  if [ "$ALICE_PROOF_AAF_XROOTD_SYSTEM_CN" != "$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrootd/system.cnf" ]; then
    ln -s $ALICE_PROOF_AAF_XROOTD_SYSTEM_CN $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrootd/system.cnf
  fi

  # replace mymssxfr_extsite.sh with xrddm
  mv $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/scripts/vMSS/mymssxfr_extsite.sh $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/scripts/vMSS/mymssxfr_extsite.sh.orig
  cp $THIS_DIR/scripts/vMSS/mymssxfr_extsite_xrddm.sh $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/scripts/vMSS/mymssxfr_extsite.sh.xrddm
  ln -s $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/scripts/vMSS/mymssxfr_extsite.sh.xrddm $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/scripts/vMSS/mymssxfr_extsite.sh

  # tmp hack so xalienfs works
  InstallExtraSECSSL

}

function GenerateXRDDMConfigs() {

  # xrd-dm.cfg
  echo "# generated `date`" > $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrddm.cfg
#   echo "xrddm.DebugLevel=1" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrd-dm.cfg
#   echo "xrddm.LogDir=/tmp/xrddm_log" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrd-dm.cfg
  mkdir -p $ALICE_PROOF_AAF_DIR/logs/xrddm > /dev/null 2>&1
#   echo "xrddm.LogDir=$ALICE_PROOF_AAF_DIR/logs/xrddm" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrd-dm.cfg
#   echo "xrddm.CreateAliEToken=1" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrd-dm.cfg
  echo "xrddm.AliEnUserName=proof" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrddm.cfg
  echo "xrddm.RenewCertificateTimeHHMM=1:00" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrddm.cfg
  echo "xrddm.CertificateDir=$ALICE_PROOF_AAF_DIR/.globus" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrddm.cfg

  echo "xrddm.XrdOptions=-v -f -S 15 -DIConnectTimeout 30 -DIConnectTimeoutWan 30 -DIFirstConnectMaxCnt 3 -DIMaxRedirectcount 10" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrddm.cfg
  FILENAME="monalisa_`echo $ALICE_PROOF_AAF_XROOTD_SE_NAME | awk -F ':' '{print $3}'`.conf"
#   echo "xrddm.AppMonConfig=$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/$FILENAME" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrd-dm.cfg
  echo "xrddm.AppMonConfig=http://aaf.cern.ch/config/$FILENAME" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrddm.cfg
  echo "xrddm.AppMonName=${ALICE_PROOF_AAF_XROOTD_SE_NAME}_xrddm" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrddm.cfg
  echo "xrddm.GlobalRedirector=alice-gr01.cern.ch" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrddm.cfg
  echo "xrddm.ROOTCpMacro=$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrddm.cfg
  echo "$ALICE_PROOF_AAF_XROOTD_MONALISA:8884" > "$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/$FILENAME"

}

function GenerateRootMacroCp() {
  echo "Int_t CpMacro(TString from, TString to) {" > $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
  echo "" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
  echo "  if (from.IsNull() || to.IsNull()) return 1;" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
  echo "" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
#   echo "  // don't use TFile::Cp in case of zip file" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
#   echo "  if (from.Contains(\".zip\")) return 2;" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
#   echo "" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
  echo "  Printf(\"TFile::Cp(\\\"%s\\\",\\\"%s\\\")\",from.Data(),to.Data()); " >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
  echo "" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
  echo "  if (from.Contains(\"alien:\/\/\")) TGrid::Connect(\"alien:\/\/\");" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
  echo "" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
  echo "  // ! operator since TFile::Cp returns kTRUE(1) in case of success" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
  echo "  return (!TFile::Cp(from.Data(),to.Data()));" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
  echo "}" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
  echo "" >> $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/CpMacro.C
}

function GenerateAfdsmgrdConfig() {
  # afdsmgrd
  mkdir -p $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/sysconfig > /dev/null 2>&1
  CURRFILE="$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/sysconfig/afdsmgrd"

  echo "# generated `date`" > $CURRFILE
  echo "" >> $CURRFILE
 
  echo "export AFDSMGRD_PROG=$ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/bin/afdsmgrd" >> $CURRFILE
  echo "export AFDSMGRD_CONF=$ALICE_PROOF_AAF_DIR/proof/xproofd/prf-main.cf" >> $CURRFILE

  echo "export AFDSMGRD_LIBS=$ALICE_PROOF_AAF_DIR/root/lib:$ALICE_PROOF_AAF_DIR/alien/api/lib:$ALICE_PROOF_AAF_DIR/alien/api/lib64:$ALICE_PROOF_AAF_DIR/alien/lib:$ALICE_PROOF_AAF_DIR/alien/lib64" >> $CURRFILE
  echo "export AFDSMGRD_LOGDIR=$ALICE_PROOF_AAF_DIR/logs/afdsmgrd" >> $CURRFILE
  echo "export AFDSMGRD_LOGFILE=\$AFDSMGRD_LOGDIR/afdsmgrd.log" >> $CURRFILE
  echo "export AFDSMGRD_PIDFILE=/var/run/afdsmgrd" >> $CURRFILE
  echo "export AFDSMGRD_USER=root" >> $CURRFILE
#   echo "export AFDSMGRD_USER=$ALICE_PROOF_AAF_USER" >> $CURRFILE
  echo "export AFDSMGRD_GROUP=$ALICE_PROOF_AAF_GROUP" >> $CURRFILE
  echo "export AFDSMGRD_DEBUG=0" >> $CURRFILE

  cp -f $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/init.d/afdsmgrd $ALICE_PROOF_AAF_DIR/scripts/afdsmgrd.init.d
  cp -f $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/sysconfig/afdsmgrd $ALICE_PROOF_AAF_DIR/scripts/afdsmgrd.sysconfig
 
}

InstallXrootdVMSS
GenerateXRDDMConfigs
#GenerateAfdsmgrdConfig
GenerateRootMacroCp

cp -f $THIS_DIR/xrddm_env.sh $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrddm_env.sh

source $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME/etc/xrddm_env.sh $ALICE_PROOF_AAF_DIR/$AAF_XROOTD_NAME
# xrdgsiproxy init -cert $ALICE_PROOF_AAF_DIR/.globus/usercert.pem -key $ALICE_PROOF_AAF_DIR/.globus/userkey.pem
# alien-token-init proof

echo " DONE installing $THIS_AAF_PACKAGE on `hostname -f` ..."
